#######################################
##      Replicating Table 6          ##
##      Post-1994 Scandals          ##
##   in Hamel and Miller (2018)      ##
#######################################

install.packages(c("Matching", "rgenoud"))
library(Matching)
library(rgenoud)

scandal <- read.csv("scandal_dta.csv")

## Post-1994 Scandals

scandal_post94 = subset(scandal, post94 == 1)

attach(scandal_post94)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_post94 <- GenMatch(Tr = scandal_id, X = covariates_match, 
                        BalanceMatrix = covariates_balance, M = 3, 
                        pop.size = 12500, max.generations = 10, wait.generations = 5, 
                        hard.generation.limit = FALSE, int.seed = 656, unif.seed = 589, 
                        fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                        replace = TRUE, ties = FALSE)

effect_post94_vote <- Match(Y = votepct, Tr = scandal_id, 
                           X = covariates_match, M = 3, 
                           exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                           replace = TRUE, ties = FALSE, version = "fast", 
                           Weight.matrix = match_post94)
summary(effect_post94_vote)

effect_post94_win <- Match(Y = win, Tr = scandal_id, 
                          X = covariates_match, M = 3, 
                          exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                          replace = TRUE, ties = FALSE, version = "fast", 
                          Weight.matrix = match_post94, 
                          match.out = effect_post94_vote)
summary(effect_post94_win)

effect_post94_money <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                            X = covariates_match, M = 3, 
                            exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                            replace = TRUE, ties = FALSE, version = "fast", 
                            Weight.matrix = match_post94, 
                            match.out = effect_post94_vote)
summary(effect_post94_money)

balance_post94 <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                dwnom1 + seniority + les + dpres + 
                                logged_pre_sum_infl + chall_quality, 
                              match.out = effect_post94_vote,
                              nboots = 5000)

detach(scandal_post94)

## Pre-1994 Scandals

scandal_pre94 = subset(scandal, post94 == 0)

attach(scandal_pre94)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_pre94 <- GenMatch(Tr = scandal_id, X = covariates_match, 
                         BalanceMatrix = covariates_balance, M = 3, 
                         pop.size = 12500, max.generations = 10, wait.generations = 5, 
                         hard.generation.limit = FALSE, int.seed = 913, unif.seed = 781, 
                         fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                         replace = TRUE, ties = FALSE)

effect_pre94_vote <- Match(Y = votepct, Tr = scandal_id, 
                            X = covariates_match, M = 3, 
                            exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                            replace = TRUE, ties = FALSE, version = "fast", 
                            Weight.matrix = match_pre94)
summary(effect_pre94_vote)

effect_pre94_win <- Match(Y = win, Tr = scandal_id, 
                           X = covariates_match, M = 3, 
                           exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                           replace = TRUE, ties = FALSE, version = "fast", 
                           Weight.matrix = match_pre94, 
                           match.out = effect_pre94_vote)
summary(effect_pre94_win)

effect_pre94_money <- Match(Y = logged_pre_sum_infl, Tr = scandal_id, 
                             X = covariates_match, M = 3, 
                             exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                             replace = TRUE, ties = FALSE, version = "fast", 
                             Weight.matrix = match_pre94, 
                             match.out = effect_pre94_vote)
summary(effect_pre94_money)

balance_pre94 <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                 dwnom1 + seniority + les + dpres + 
                                 logged_pre_sum_infl + chall_quality, 
                               match.out = effect_pre94_vote,
                               nboots = 5000)

detach(scandal_pre94)
